<?php

class CategoriesController extends AppController {

    var $name = 'Categories';
    var $helpers = array('Html', 'Session', 'Javascript', 'Link');
    var $uses = array('User');

    function beforeFilter() {
        $this->Auth->allow(array('*'));
        parent::beforeFilter();
    }

    function index() {
        echo __METHOD__;
        $this->layout = "default";
        /* Start Categories Section */
        $categories = $this->_get_parent_categories();
        $x = 0;
        if (isset($categories) and ($categories)) {
            foreach ($categories as $category) {
                $subcats = $this->_sub_catecategories($category['Category']['id']);
                if (isset($subcats) and ($subcats)):
                    $categories[$x]['Category']['Subcategories'] = $subcats;
                endif;
                $x++;
            }
            $this->Set('categories', $categories);
        }
        /* End Categories Section */
    }

    function subcategories($cat_slug ="") {
        echo __METHOD__;
        $cat_id = $this->__get_slug_to__id($cat_slug, "Category");
        $Category = $this->__get_details($cat_id, 'Category');
        $this->set('cat_slug', $cat_slug);
        $this->Set("Category", $Category);



        $departments = $this->Department->find('all', array('conditions' => 'Department.cat_id Like "%' . $cat_id . '%"'));
        $this->Set('departments', $departments);

        $subcats = $this->_get_subcatecategories($cat_id, "");
        if (isset($subcats) and ($subcats)):
            $this->Set('refer_categories', $subcats);
            $c = 0;
            foreach ($subcats as $subcat) {
                /* $products = $this->Product->find('all',array('conditions'=>'Product.cat_id ='.$subcat['Category']['id'].'',
                  'fields'=>'Product.slug, Product.name'
                  )); */
                $subcats_inner_it = $this->_get_subcatecategories($subcat['Category']['id'], 4);
                if (isset($subcats_inner_it) and ($subcats_inner_it)):
                    $subcats[$c]['Category']['subcategories'] = $subcats_inner_it;
                endif;
                $c++;
            }
            //pr($subcats);
            //exit;
            $this->Set('subcats', $subcats);
        endif;
        $brands = $this->Brand->find('all', array('conditions' => 'Brand.cat_id Like "%' . $cat_id . '%"'));
        //pr($brands);
        //exit;
        if (isset($brands) and ($brands)) {
            foreach ($brands as $brand) {
                $custum_brands[] = $brand;
            }
            if (isset($custum_brands) and ($custum_brands)) {
                $this->Set('custum_brands', $custum_brands);
            }
        }

        $conditions = '(Product.cat_id="' . $cat_id . '") and Product.status="1"';
        $products = $this->_getProducts($conditions, "");
        $this->Set('products', $products);
    }

    function get_quantities() {
        echo __METHOD__;
        $this->layout = "ajax";
        //echo "jojo";
        $params = $this->params;
        $option_value = $params['url']['option_value'];
        $product_id = $params['url']['product_id'];
        $qty = $this->OptionValue->find('first', array('conditions' => 'OptionValue.option_value="' . $option_value . '" 
			and OptionValue.product_id="' . $product_id . '"', 'fields' => 'OptionValue.qty, OptionValue.id'));
        if (isset($qty) and ($qty)) {
            $this->Set('qty', $qty);
        }
    }

    function get_specifications() {
        echo __METHOD__;
        $this->layout = "ajax";
        $params = $this->params;
        if (isset($params['form']['label_checkbox_value']) and ($params['form']['label_checkbox_value'])) {
            $exist = $this->Session->read('' . $params['form']['label_checkbox_value'] . '');
            if (isset($exist) and ($exist == 1)) {
                $this->Session->delete('' . $params['form']['label_checkbox_value'] . '');
                $this->set('ans', 'true');
            } else {
                $this->Session->write('' . $params['form']['label_checkbox_value'] . '', 1);
                $this->set('ans', 'false');
            }
        }
    }

}
